package com.gt.quality.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gt.quality.base.BusinessSelfException;
import com.gt.quality.config.SpringBeanUtil;
import com.gt.quality.system.entity.SysRole;
import com.gt.quality.system.entity.SysRoleMenu;
import com.gt.quality.system.entity.SysUserRole;
import com.gt.quality.system.mapper.SysRoleMapper;
import com.gt.quality.base.pojo.BaseLimitRequest;
import com.gt.quality.system.service.SysRoleMenuService;
import com.gt.quality.system.service.SysRoleService;
import com.gt.quality.system.service.SysUserRoleService;
import jakarta.annotation.Resource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.List;

/**
 * 角色信息表(SysRole)表服务实现类
 *
 * @author makejava
 * @since 2026-11-25 16:30:08
 */
@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Resource
    @Lazy
    private SysUserRoleService sysUserRoleService;

    @Resource
    @Lazy
    private SysRoleMenuService sysRoleMenuService;


    /**
     * Description:  分页查询
     *
     * @param limitRequest 分页查询参数
     * @author: makeJava
     * @date: 2026-02-02 14:57:06
     * @return:com.baomidou.mybatisplus.core.metadata.IPage
     */
    @Override
    public IPage<SysRole> queryLimitPage(BaseLimitRequest<SysRole> limitRequest) {
        SysRole requestBody = limitRequest.getRequestBody();
        IPage<SysRole> page = getByPageParam(limitRequest);
        JSONObject thisObj = JSONObject.from(requestBody);
        QueryWrapper<SysRole> queryWrapper = queryWrapperBuilder(thisObj, limitRequest);
        queryWrapper.orderByDesc("role_sort");
        return sysRoleMapper.selectPage(page, queryWrapper);
    }

    /**
     * Description:  批量新增优化版
     *
     * @param entityList entityList
     * @author: makeJava
     * @date: 2026-02-02 19:54:59
     */
    @Override
    public Integer saveBatchByEasyBaseMapper(List<SysRole> entityList) {

        return sysRoleMapper.insertBatchSomeColumn(entityList);
    }

    @Override
    public List<SysRole> selectByUserId(Long userId) {
        return sysRoleMapper.selectByUserId(userId);
    }

    @Override
    public void checkUserBindRole(List<Long> roleIds) {
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SysUserRole::getRoleId, roleIds);
        List<SysUserRole> list = sysUserRoleService.list(wrapper);
        if (CollUtil.isNotEmpty(list)) {
            throw new BusinessSelfException("还有用户正在使用当前角色");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean warnDeleteOperation(List<Long> idList) {
        // 删除角色
        sysRoleMapper.deleteByIds(idList);
        // 删除角色绑定的菜单
        LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SysRoleMenu::getRoleId, idList);
        sysRoleMenuService.remove(wrapper);
        return true;
    }


    /**
     * 往上传递 SqlSessionFactory
     *
     * @return SqlSessionFactory
     */
    @Override
    public SqlSessionFactory getSqlSessionFactory() {
        return super.getSqlSessionFactory();
    }

    /**
     * 往上传递 Mapper
     *
     * @return Mapper
     */
    @Override
    public Class<?> getThisMapperClass() {
        return SysRoleMapper.class;
    }

    /**
     * 往上传递 Transaction
     *
     * @return Transaction
     */
    @Override
    public TransactionTemplate getSpringTx() {
        return SpringBeanUtil.getBean(TransactionTemplate.class);
    }
}


